package com.chx.three.mama.view;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.chx.three.mama.R;
import com.chx.three.mama.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 *  用来获取照片，添加照片的功能类
 */
public class PicLayout extends LinearLayout {
    //可以存放的照片数量
    private  int mPinNum=6;
    //获取图片的按钮
    private Context mContext;
    private int mImgMargin;
    private int mWidth;
    private ArrayList<ImageView> images;
    private Dialog obtainDialog;
    private ArrayList<Bitmap> mBitmaps;

    public PicLayout(Context context) {
        this(context, null);
    }

    public PicLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext=context;
        mImgMargin= DensityUtil.dip2px(mContext,2);
        setGravity(Gravity.CENTER_VERTICAL);
        images=new ArrayList<>();
        for (int i = 0; i < mPinNum; i++) {
            ImageView image=new ImageView(mContext);
            image.setId(i);
            image.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            images.add(image);
            addView(image);
        }
        images.get(0).setImageResource(R.drawable.icon_addpic_unfocused);
        obtainPin(null,images.get(0));
        mBitmaps = new ArrayList<>();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            mWidth = MeasureSpec.getSize(widthMeasureSpec);
            int width=(mWidth-(mPinNum*2))/mPinNum;
        LayoutParams lp = new LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT);
        lp.leftMargin=mImgMargin;
        lp.rightMargin=mImgMargin;
        lp.topMargin=mImgMargin;
        lp.bottomMargin=mImgMargin;

        for (int i = 0; i < mPinNum; i++) {
            lp.weight=1;
            images.get(i).setLayoutParams(lp);
            updateViewLayout(images.get(i), lp);
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 获取图片的事件及撤销之前的点击事件
     */
    public void obtainPin(View oldView,View view){
        if(oldView!=null){
            oldView.setOnClickListener(null);
        }
        if(view!=null){
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    showPopupWindow(v);
                }
            });
        }
    }
    public void showPopupWindow(View v){
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.layout_popup_obtain_picture, null);
        obtainDialog = new Dialog(mContext);
        obtainDialog.setContentView(inflate);
        obtainDialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.color.transparent));
        obtainDialog.show();
        View album = inflate.findViewById(R.id.obtain_pic_from_pics);
        View camera = inflate.findViewById(R.id.obtain_pic_from_cram);
        album.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mContext instanceof OnObtainPictureListener){
                    ((OnObtainPictureListener)mContext).onObtainAlbum();
                }
            }
        });
        camera.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mContext instanceof OnObtainPictureListener){
                    ((OnObtainPictureListener)mContext).onObtainCamera();
                }
            }
        });
        //设置点击获取图片
    }
    public interface OnObtainPictureListener{
        void onObtainAlbum();
        void onObtainCamera();
    }

    //当前的照片数量
    int mCurrentPicNum;
    /**
     * 添加照片
     * @param bitmap
     */
    public void addImage(Bitmap bitmap){
        if(bitmap==null){
            return;
        }

        //添加图片的索引
        if(mCurrentPicNum >= mPinNum-1){
            images.get(mCurrentPicNum).setImageBitmap(bitmap);
            obtainPin(images.get(mCurrentPicNum), null);
            mBitmaps.add(mCurrentPicNum,bitmap);
        }else{
            images.get(mCurrentPicNum).setImageBitmap(bitmap);
            images.get(mCurrentPicNum+1).setImageResource(R.drawable.icon_addpic_unfocused);
            obtainPin(images.get(mCurrentPicNum), images.get(mCurrentPicNum+1));
            mBitmaps.add(mCurrentPicNum,bitmap);
            mCurrentPicNum++;
        }
    }
    public void dismissDialog(){
        if(obtainDialog.isShowing()){
            obtainDialog.dismiss();
        }
    }
    /**
     * 判断照片是否已经达到6张
     * @return
     */
    public boolean isFull(){
        return mCurrentPicNum>=mPinNum-1;
    }
    public int getPicNum(){
        return mCurrentPicNum+1;
    }
    public List<Bitmap>  getBitmapList(){
        return mBitmaps;
    }

}
